<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确定</AjaxButton>
    </div>
  </div>
</template>

<script>
import { addTestDriveCars, editTestDriveCars, getBrandList, getSeriesList, getModelList, getPackageList, getColorsList, getColorsListByOption } from '@/api/potenCus/basedata/searchTestDriveCars';
import { dictionary } from '@/utils/dictMixin';
import AjaxButton from '@/components/AjaxButton';
import { getOptionListDictOEM } from '@/api/common/cars/masterDataCars';

export default {
  name: 'ChannelModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    },
    hasData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      isEditId: '',
      sort: '',
      isMainIntent: ''
    };
  },
  created() {
    this.init();
    this.getDictBrandList();
  },
  methods: {
    init() {
      let { rowData } = this;
      console.log('编辑的值', this.type, rowData);
      if (this.type === 'add') {
      }
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.isEditId = rowData.clueIntentId;
        this.isMainIntent = rowData.isMainIntent;
        this.sort = rowData.$index;
        this.formList.forEach(x => {
          if (x.fieldName === 'intentBrandId') {
            x.initialValue = rowData['intentBrand'];
          } else if (x.fieldName === 'intentSeriesId') {
            x.initialValue = rowData['intentSeries'];
            if (rowData['intentBrand']) {
              getSeriesList(rowData['intentBrand']).then(res => {
                // console.log('编辑车系', res)
                x.itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
              });
            }
          } else if (x.fieldName === 'intentModelId') {
            x.initialValue = rowData['intentModelId'];
            if (rowData.intentBrand && rowData.intentSeries) {
              getModelList(rowData.intentBrand, rowData.intentSeries).then(res => {
                // console.log('编辑车型', res)
                x.itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
              });
            }
          } else if (x.fieldName === 'intentPackageId') {
            x.initialValue = rowData['intentPackageId'];
            if (rowData.intentModelId) {
              getPackageList(rowData.intentModelId).then(res => {
                // console.log('编辑配置', res)
                x.itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
              });
            }
          } else if (x.fieldName === 'intentColorId') {
            x.initialValue = rowData['intentColorId'];
            if (rowData.intentPackageId) {
              getColorsList(rowData.intentPackageId).then(res => {
                // console.log('编辑颜色', res)
                x.itemList = res.data.map(x => ({ value: x.colorId, text: x.colorName }));
              });
            }
          }
          x.initialValue = rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '意向品牌',
          fieldName: 'intentBrandId',
          placeholder: '请选择',
          // disabled: this.type === 'edit',
          itemList: [],
          rules: [{ required: true, message: '请选择意向品牌', trigger: 'blur' }],
          change: val => {
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '意向车系',
          fieldName: 'intentSeriesId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择意向车系', trigger: 'blur' }],
          change: val => {
            this.changeSeriesSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '意向车型',
          fieldName: 'intentModelId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择意向车型', trigger: 'blur' }],
          change: val => {
            this.changeModelSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '意向配置',
          fieldName: 'intentPackageId',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            //this.changeColorSelect(val);
            this.changePackageSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '意向选装',
          fieldName: 'intentOptionId',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.changeColorSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '意向颜色',
          fieldName: 'productId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '购买备注',
          fieldName: 'remark',
          placeholder: '请输入'
        }
      ];
    },
    formChangeHandle(val) {
      // console.log("val",val)
      this.saveData = val;
    },
    ifHasData(data) {
      let { hasData } = this;
      let { intentBrandId, intentSeriesId, intentModelId, intentPackageId, intentOptionId, intentColorId } = data;
      let aimString = [intentBrandId, intentSeriesId, intentModelId, intentPackageId, intentOptionId, intentColorId].toString();
      // console.log('已有意向车辆', hasData)
      // console.log('当前添加意向', aimString)
      return hasData.includes(aimString);
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        // console.log("意向保存",this.formList)
        console.log('保存的是什么', this.saveData);
        // if(){
        let params = {
          intentBrandId: this.formList[0].initialValue,
          isMainIntent: 10041002,
          intentSeriesId: this.formList[1].initialValue,
          intentModelId: this.formList[2].initialValue,
          intentPackageId: this.formList[3].initialValue,
          intentOptionId: this.formList[4].initialValue,
          productId: this.formList[5].initialValue,
          intentColorId: '',
          interiorId: '',
          isEditId: this.isEditId,
          sort: this.sort,
          remark: this.formList[6].initialValue
        };
        // }
        let isOk = this.ifHasData(params);
        if (isOk) {
          this.$message.error('该意向车型已存在');
          return;
        }
        this.$emit('YXHandle', params);
        this.closeDialog('refresh');
      } else {
        this.$message.info('请填写必填项!');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async getDictBrandList() {
      const res = await getBrandList();
      // console.log("品牌YX",res)
      // console.log("品牌数量", res.data.length)
      if (res.resultCode === 200) {
        if (res.data.length == 1) {
          // this.formList.find(x=>x.fieldName ==='intentBrandId').initialValue = res.data[0].brandId
          if (res.data[0].isValid == 10041001) {
            this.formList[0].initialValue = res.data[0].brandId;
            const res1 = await getSeriesList(res.data[0].brandId);
            console.log('res1', res1);
            this.formList[1].itemList = res1.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
          }
        }
        res.data.forEach(x => {
          // isValid为10041002时表示此品牌无效，则不显示20191206 || x.isValid == null
          if (x.isValid == 10041001) {
            // console.log(x,"x")
            this.formList[0].itemList.push({ value: x.brandId, text: x.brandName });
          }
        });
        // this.formList[0].itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList[1].initialValue = '';
        this.formList[1].itemList = [];
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
        this.formList[5].initialValue = '';
        this.formList[5].itemList = [];
      } else {
        const res = await getSeriesList(brandsid);
        if (res.resultCode === 200) {
          // console.log("车系YX",res)
          this.formList[1].itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
        if (this.formList[1].initialValue) {
          this.formList[1].initialValue = '';
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';

          this.formList[5].initialValue = '';
        }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList[0].initialValue;
      if (!brandsid || !seriessid) {
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
        this.formList[5].initialValue = '';
        this.formList[5].itemList = [];
      } else {
        const res = await getModelList(brandsid, seriessid);
        if (res.resultCode === 200) {
          // console.log("车型YX",res)
          this.formList[2].itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
        if (this.formList[2].initialValue) {
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';

          this.formList[5].initialValue = '';
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.formList[0].initialValue;
      const seriessid = this.formList[1].initialValue;
      if (!brandsid || !seriessid || !modelId) {
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
        this.formList[4].initialValue = '';
        this.formList[4].itemList = [];
        this.formList[5].initialValue = '';
        this.formList[5].itemList = [];
      } else {
        const res = await getPackageList(modelId);
        if (res.resultCode === 200) {
          // console.log("配置YX",res)
          this.formList[3].itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
        if (this.formList[3].initialValue) {
          this.formList[3].initialValue = '';
          this.formList[4].initialValue = '';
          this.formList[5].initialValue = '';
        }
      }
    },
    //CMS 增加根据配置查询选装信息
    async changePackageSelect(packageId) {
      const brandsid = this.formList.find(item => item.fieldName === 'intentBrandId').initialValue;
      const seriessid = this.formList.find(item => item.fieldName === 'intentSeriesId').initialValue;
      const modelId = this.formList.find(item => item.fieldName === 'intentModelId').initialValue;
      if (!brandsid || !seriessid || !modelId || !packageId) {
        this.formList.find(item => item.fieldName === 'intentPackageId').initialValue = '';
        this.formList.find(item => item.fieldName === 'intentPackageId').itemList = [];
      } else {
        const res = await getOptionListDictOEM(packageId);
        if (res.resultCode === 200) {
          this.formList.find(item => item.fieldName === 'intentOptionId').itemList = res.data.map(x => ({ value: x.optionId, text: x.optionName }));
        }
        if (this.formList[4].initialValue) {
          this.formList[4].initialValue = '';
          this.formList[5].initialValue = '';
        }
      }
    },
    async changeColorSelect(optionId) {
      const brandsid = this.formList[0].initialValue;
      const seriessid = this.formList[1].initialValue;
      const modelId = this.formList[2].initialValue;
      const packageId = this.formList[3].initialValue;
      if (!brandsid || !seriessid || !modelId || !packageId || !optionId) {
        this.formList[5].initialValue = '';
        this.formList[5].itemList = [];
      } else {
        const res = await getColorsListByOption(optionId);
        if (res.resultCode === 200) {
          console.log('颜色YX', res);
          this.formList[5].itemList = res.data.map(x => ({ value: x.productId, text: x.colorName }));
        }
        if (this.formList[5].initialValue) {
          this.formList[5].initialValue = '';
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
